package fr.upmf.miam.imageannotation;

import fr.upmf.miam.imageannotation.ImageAnnotationProvider.C;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore.Images;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * ViewContacts 
 * Affiche le contenu de /people/#/picture (les images d'une personne)
 * 
 * @author aurelie
 */
public class ViewContacts extends Activity implements OnItemClickListener {


    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_view_contacts);
	
	Uri uri = Uri.withAppendedPath(ImageAnnotationContract.People.CONTENT_URI, this.getIntent().getData().getPathSegments().get(1)); 
	
	//on recupere ses infos et on affiche
	Cursor c = getContentResolver().query(uri, null, null, null, null);
	if(c.getCount()==0) {
	    throw new IllegalArgumentException(("contact inconnu : " + uri.toString()));
	}
	c.moveToFirst();

	//son nom
    		TextView name = (TextView) findViewById(R.id.contactName);
    		name.setText(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
	
	//son image    		
    		QuickContactBadge photo = (QuickContactBadge) findViewById(R.id.contactPhoto);
    		Bitmap img = null;
    		
    		if(c.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)!=-1) {
        		byte[] tmp = null;
        		Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, c.getLong(c.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)));
        		Cursor cursor = getContentResolver().query(photoUri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);
    
        		try { if (cursor.moveToFirst()) { tmp = cursor.getBlob(0); } } catch (Exception e) { }  finally { cursor.close(); }           
        		if (tmp != null) {
        		    img = BitmapFactory.decodeByteArray(tmp,0,tmp.length);
    	        
        		    photo.assignContactUri(Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY));
        		}
    		}
    		if(img == null) {
    		    img = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.unknown);
    		}
    		photo.setImageBitmap(img);
	
	//liste des images auquelles il est associe
    		Uri requestUri = Uri.withAppendedPath(uri, "/" + C.People.Picture.CONTENT_PATH);
    		Cursor cs = getContentResolver().query(requestUri, new String[] { 
    			ImageAnnotationProvider.SQL_getId(C.Picture.URI) + " as _id",
    			C.Picture.URI
    		}, null, null, null);
    		GridView g = (GridView) findViewById(R.id.gridview);
    		g.setAdapter(new ImageAnnotationGalleryAdapter(this, cs));
    		g.setOnItemClickListener(this);
    }


    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.view_contacts, menu);
	return true;
    }


    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    		//On recupère l'uri interne de l'image
		Uri uriExterne = Uri.withAppendedPath(Images.Media.EXTERNAL_CONTENT_URI, arg3+"");
		Cursor c = getContentResolver().query(
			ImageAnnotationContract.Picture.CONTENT_URI, 
			new String[] { ImageAnnotationContract.Picture._ID }, 
			C.Picture.URI + "=?", new String[] { uriExterne.toString() }, 
			null);
		c.moveToFirst();
		Uri uri = Uri.withAppendedPath(ImageAnnotationContract.Picture.CONTENT_URI, c.getString(0));
		
		// Création de l'intent
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_EDIT);
		intent.setDataAndType(uri, "image/*");
		startActivity(intent);
    }
}